﻿using K8.EF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.WebSockets;
using System.Text;
using System.Threading.Tasks;

namespace KingerRobot.MexicoSailunMDC.SpiService;

internal class BWcsOutputLog : IWcsOutputLog
{
    public POWcsOutputLogs GetWcsOutputLogByTaskId(long taskId)
    {
        var db = Db.Default;
        var result = db.WcsOutputLogss.Where(t => t.TaskId == taskId).FirstOrDefault();
        return result;
    }

    public int UpdateWcsOutputLogById(long id)
    {
        return 0;
    }
    public int CreateOrUpdateWcsOutputLog(long taskId, int taskNo, int logType, string remark = "")
    {
        var db = Db.Default;
        var result = 0;
        try
        {
            if (logType == (int)EnumOutputLogPlan.CrnStart)
            {

                var sql = $@"INSERT INTO [dbo].[wcs_output_logs]([task_id],[task_no],[crn_start_time],[remark])
             VALUES('{taskId}','{taskNo}',getdate(),'CrnStart{remark}-')";

                DbHelper dbHelper = new();

                dbHelper.CommandText = sql;
                dbHelper.CommandType = System.Data.CommandType.Text;

                result = dbHelper.ExecuteNonQuery();
                return result;
            }
            else //修改数据
            {
                var outputLogs = db.WcsOutputLogss.Where(t => t.TaskId == taskId).OrderByDescending(t => t.CreateTime).FirstOrDefault();
                if (outputLogs is not null)
                {
                    var buildSql = "";
                    switch (logType)
                    {
                        case (int)EnumOutputLogPlan.CrnEnd:
                            buildSql = $" set crn_end_time=getdate(),remark=remark+'CrnEnd-' ";
                            break;
                        case (int)EnumOutputLogPlan.LineSelectStart:
                            buildSql = $" set line_select_start_time=getdate(),remark=remark+'LineSelectStart-' ";
                            break;
                        case (int)EnumOutputLogPlan.LineSelectEnd:
                            buildSql = $" set line_select_end_time=getdate(),remark=remark+'LineSelectEnd-' ";
                            break;
                        case (int)EnumOutputLogPlan.LineCreateStart:
                            buildSql = $" set line_create_start_time=getdate(),remark=remark+'LineCreateStart-' ";
                            break;
                        case (int)EnumOutputLogPlan.LineCreateEnd:
                            buildSql = $" set line_create_end_time=getdate(),remark=remark+'LineCreateEnd-' ";
                            break;
                        case (int)EnumOutputLogPlan.LineExcuteStart:
                            buildSql = $" set line_excute_start_time=getdate(),remark=remark+'LineExcuteStart {remark}-' ";
                            break;
                        case (int)EnumOutputLogPlan.LineExcuteEnd:
                            buildSql = $" set line_excute_end_time=getdate() ,remark=remark+'LineExcuteEnd-' ";
                            break;

                    }
                    var sql = $@"UPDATE [dbo].[wcs_output_logs] {buildSql} WHERE task_id={taskId}";

                    DbHelper dbHelper = new();
                    dbHelper.CommandText = sql;
                    dbHelper.CommandType = System.Data.CommandType.Text;

                    result = dbHelper.ExecuteNonQuery();
                    return result;
                }

            }
            return result;
        }
        catch (Exception ex)
        {
            WriteLog($"插入出库放胎位监控日志异常-{ex.ToString()}", true);
            return 0;
        }
    }
    public int CreateWcsOutputLog(POWcsOutputLogs wcsOutputLogs)
    {
        var db = Db.Default;
        var result = db.WcsOutputLogss.Insert(wcsOutputLogs);
        return result;
    }

}

